let axios = require('axios').default;
let FeedParser = require('feedparser');
let http = require('lib.http');
const { resolve } = require('path');
const { Readable } = require("stream");

module.exports = {
    fetch: function(baseUrl, proxy) {
        return http.get(baseUrl, undefined, proxy)
            .then(response => {
                return Promise.resolve(response.data)
            })
            .catch(err => {
                return Promise.reject(err);
            });
    },

    parserjson: function(xml) {
        return new Promise((resolve, reject) => {
            var feedparser = new FeedParser();
            var feed = { meta: {}, items: [] };
            var err = false;
            feedparser.on('error', function(e) {
                err = true;
                // console.log({ e })
            });

            feedparser.on('readable', function() {
                var stream = this; // `this` is `feedparser`, which is a stream
                var item;
                while (item = stream.read()) {
                    delete item.meta; // meta不需要每条记录保留；
                    feed.items.push(item);
                }
            });

            feedparser.on('end', function(e) {
                // console.log(this);
                if (err) {
                    reject({ message: "Not a feed" });
                } else {
                    feed.meta = this.meta;
                    resolve(feed)
                };
            });

            let readable = Readable.from([xml])
            readable.pipe(feedparser);
        }).then(data => {
            return new Promise((resolve, reject) => {
                // console.log(data);
                let result = {
                    meta: data.meta,
                    ListRecords: []
                };

                data.items.forEach(item => {
                    // console.log(item);
                    let metadata = {
                        header: {
                            identifier: item.guid || item.link,
                            datestamp: item.date || item.pubDate || new Date()
                        },
                        JMETA: {
                            schema: 'JMETA_DC',
                            title: [
                                { text: item.title }
                            ],
                            identifier: [
                                { text: item.link || item.guid, url: item.link || item.guid }
                            ],
                            description: [
                                { text: item.description }
                            ]
                        },
                        source: item
                    }

                    if (data.copyright) {
                        metadata.JMETA.rights = [
                            { text: item.copyright }
                        ]
                    }

                    if (item.enclosures) {
                        item.enclosures.forEach(enclosure => {
                            if (enclosure.url) enclosure.text = enclosure.url;
                            metadata.JMETA.description.push(enclosure);
                        })
                    }

                    if (item.guid && item.guid !== item.link) {
                        metadata.JMETA.identifier.push({ text: item.guid });
                    }

                    if (item.source && Object.keys(item.source).length > 0) {
                        metadata.JMETA.source = [{
                            text: item.source.title || item.source.url
                        }];
                    }

                    if (item.permalink) {
                        metadata.JMETA.identifier.push({ text: item.permalink });
                    }

                    if (item.date || item.pubDate) {
                        metadata.JMETA.date = [
                            { text: item.date || item.pubDate }
                        ]
                    }

                    if (item.author) {
                        metadata.JMETA.creator = [];
                        item.author.split(',').forEach(creator => {
                            metadata.JMETA.creator.push({ text: creator });
                        })
                    } else if (item['atom:author']) {
                        item['atom:author'].forEach(a => {
                            if (!metadata.JMETA.creator) metadata.JMETA.creator = [];
                            metadata.JMETA.creator.push({ text: a['name']['#'] })
                        })
                    }

                    if (item.categories) {
                        metadata.header.setSpec = item.categories;
                        if (!metadata.JMETA.subject) metadata.JMETA.subject = [];
                        item.categories.forEach(category => {
                            metadata.JMETA.subject.push({
                                text: category
                            })
                        });
                        // metadata.JMETA.subject.push({ category: item.categories });
                    }
                    if (item.image) {
                        metadata.JMETA.description.push({ image: item.image.url, title: item.image.title });
                    }
                    if (item['media:keywords']) {

                        if (!metadata.JMETA.subject) metadata.JMETA.subject = [];
                        item['media:keywords']['#'].split(',').forEach(function(keyword) {
                            metadata.JMETA.subject.push({ text: keyword });
                        });
                    }

                    result.ListRecords.push(metadata);
                })
                resolve(result);
            })
        }).catch(err => {
            return Promise.reject(err);
        });
    }

}